package com.bingxue.edu.renewal;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.bingxue.edu.common.enums.EnrollmentStatusEnum;
import com.bingxue.edu.common.enums.TermEnum;
import com.bingxue.edu.framework.common.util.CheckUtils;
import com.bingxue.edu.management.education.service.EnrollmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;
import static com.bingxue.edu.management.education.model.entity.table.EnrollmentTableDef.ENROLLMENT;
import static com.bingxue.edu.management.education.model.entity.table.StudentTableDef.STUDENT;

/**
 * @author panbo
 * @since 2025/1/6
 */
@Slf4j
@SpringBootTest
public class Renewal2024Tool {

    @Autowired
    private EnrollmentService enrollmentService;

//    @Test
    public void fixed() throws FileNotFoundException {
        String path = "/Users/panbo/Downloads/test.xlsx";
        List<ExcelData> list = new ArrayList<>();
        File file = ResourceUtils.getFile(path);
        EasyExcel.read(file, ExcelData.class, new PageReadListener<ExcelData>(dataList -> {
            // 并且每行数据，并将其 add 至 list 中
            for (ExcelData excelData : dataList) {
                if (excelData != null) {
                    list.add(excelData);
                }
            }
        })).excelType(ExcelTypeEnum.XLSX).sheet().doRead(); // 指定 Excel 的文件后缀，开始分析读取

        for (ExcelData excelData : list) {
            // 查询退费人数
            try {
                String response = HttpUtil.post("https://www.bingxuewenhua.com/api/temp/getCancelClassCount/" + excelData.getClassId(), new HashMap<>());
                JSONObject result = new JSONObject(response);
                CheckUtils.throwIfNotEqual(result.getInt("code"), 0, "远程调用查询退课人数出错");
                JSONObject data = result.getJSONObject("data");
                Integer cancelClassStudentCount = data.getInt("count");
                excelData.setCancelClassStudentCount(cancelClassStudentCount);

                String response2 = HttpUtil.post("https://www.bingxuewenhua.com/api/temp/getStudentList/" + excelData.getClassId(), new HashMap<>());
                JSONObject result2 = new JSONObject(response2);
                CheckUtils.throwIfNotEqual(result2.getInt("code"), 0, "远程调用查询班级学员出错");
                List<LegacyStudentVO> studentVOList = result2.getBeanList("data", LegacyStudentVO.class);
                int renewalCount = 0;
                for (LegacyStudentVO studentVO : studentVOList) {
                    boolean exists = enrollmentService.queryChain()
                            .from(ENROLLMENT)
                                .leftJoin(CLASS_INFO).on(CLASS_INFO.ID.eq(ENROLLMENT.CLASS_ID))
                                .leftJoin(STUDENT).on(STUDENT.ID.eq(ENROLLMENT.STUDENT_ID))
                            .where(ENROLLMENT.STATUS.eq(EnrollmentStatusEnum.NORMAL))
                            .and(CLASS_INFO.CLASS_YEAR.eq(2025))
                            .and(CLASS_INFO.TERM.eq(TermEnum.WINTER))
                            .and(CLASS_INFO.SUBJECT.eq("math-xx"))
                            .and(STUDENT.NAME.eq(studentVO.getName()))
                            .and(STUDENT.PARENT_PHONE.eq(studentVO.getParentMobile()))
                            .exists();
                    if (exists) {
                        renewalCount += 1;
                    }
                }
                excelData.setRetainClassStudentCount(renewalCount);
                int total = excelData.getLastLessonStudentCount() + excelData.getCancelClassStudentCount();
                BigDecimal rate = new BigDecimal(renewalCount).divide(new BigDecimal(total), 2, RoundingMode.UP);
                excelData.setRetainRate(rate);
//                break;
            } catch (Exception e) {
                log.error("远程调用设置下半期续报失败", e);
            }
        }
        String outFile = "/Users/panbo/Downloads/output.xlsx";
        EasyExcel.write(outFile, ExcelData.class).sheet("0").doWrite(list);
    }

}
